﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace UnitySheet
{
    /// <summary>
    /// 锚点，用于组成单元格、控制单元格大小、单元格合并等。如果需要扩展功能请继承此类
    /// </summary>
    public class AnchorPoint : MonoBehaviour, IDragHandler
    {
        
        public int x, y;
        public delegate void AnchorPointHandler(AnchorPoint anchorPoint);
        public event AnchorPointHandler onDrag;
        public event AnchorPointHandler onMove;
        protected List<Cell> myCells = new List<Cell>();
        protected Sheet mySheet;

        /// <summary>
        /// 初始化列表时的创建锚点
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static AnchorPoint CreateOneAnchorPoint(int x, int y, Sheet sheet)
        {
            AnchorPoint anchorP = Instantiate(SheetManager.sheetManager.anchorPointPrefab);
            anchorP.mySheet = sheet;
            anchorP.transform.SetParent(sheet.transform);
            anchorP.x = x;
            anchorP.y = y;
            anchorP.name = "anchor";

            anchorP.transform.localPosition = new Vector3(x * SheetManager.sheetManager.width + SheetManager.sheetManager.cellSpace * 2 * x, -y * SheetManager.sheetManager.height - SheetManager.sheetManager.cellSpace * 2 * y, 0);

            anchorP.gameObject.SetActive(false);
            return anchorP;
        }

        /// <summary>
        /// 添加行列时的创建锚点
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="prevAnchorPoint"></param>
        /// <returns></returns>
        public static AnchorPoint CreateOneAnchorPoint(int x, int y, Sheet sheet, AnchorPoint prevAnchorPoint)
        {
            AnchorPoint anchorP = Instantiate(SheetManager.sheetManager.anchorPointPrefab);
            anchorP.mySheet = sheet;
            anchorP.transform.SetParent(sheet.transform);
            anchorP.x = x;
            anchorP.y = y;
            anchorP.name = "anchor";

            Vector3 pos = prevAnchorPoint.transform.position;

            pos.x += (SheetManager.sheetManager.width + SheetManager.sheetManager.cellSpace * 2) * (x - prevAnchorPoint.x);
            pos.y -= (SheetManager.sheetManager.height + SheetManager.sheetManager.cellSpace * 2) * (y - prevAnchorPoint.y);
            anchorP.transform.position = pos;
            anchorP.gameObject.SetActive(false);

            return anchorP;
        }

        /// <summary>
        /// 给定位置创建锚点,一般用于拆分单元格
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public static AnchorPoint CreateOneAnchorPoint(int x, int y, Sheet sheet, Vector3 pos)
        {
            AnchorPoint anchorP = Instantiate(SheetManager.sheetManager.anchorPointPrefab);
            anchorP.mySheet = sheet;
            anchorP.transform.SetParent(sheet.transform);
            anchorP.x = x;
            anchorP.y = y;
            anchorP.name = "anchor";

            anchorP.transform.position = pos;
            anchorP.gameObject.SetActive(false);
            return anchorP;
        }

        public void AddCell(Cell cell)
        {
            myCells.Add(cell);
            gameObject.SetActive(true);
        }
        public void RemoveCell(Cell cell)
        {
            myCells.Remove(cell);
            if (myCells.Count == 0)
            {
                gameObject.SetActive(false);
                bool removeY = true, removeX = true;
                for (int i = 0; i < mySheet.yNum; i++)
                {
                    if (mySheet.anchors[x][i].myCells.Count != 0)
                    {
                        removeX = false;
                        break;
                    }
                }
                for (int i = 0; i < mySheet.xNum; i++)
                {
                    if (mySheet.anchors[i][y].myCells.Count != 0)
                    {
                        removeY = false;
                        break;
                    }
                }
                if (removeY)
                {
                    mySheet.DeleteRowAnchors(y);
                }
                if (removeX)
                {
                    mySheet.DeleteColumnAnchors(x);
                }
            }
        }

        public Cell[] GetMyCells()
        {
            return myCells.ToArray();
        }

        public void SyncAnchorPoint(AnchorPoint anchorPoint)
        {
            Vector3 pos = transform.position;
            if (anchorPoint.x == x)
            {
                pos.x = anchorPoint.transform.position.x;
            }
            if (anchorPoint.y == y)
            {
                pos.y = anchorPoint.transform.position.y;
            }
            Move(pos);
        }

        public void Move(Vector3 pos)
        {
            transform.position = pos;
            if (onMove != null)
            {
                onMove(this);
            }
        }

        void IDragHandler.OnDrag(PointerEventData eventData)
        {
            Vector2 pos = Input.mousePosition;
            #region//判断拖拽是否小于最小宽度的限制,需改进
            //if (y < SheetManager.sheetManager.sheet.yNum - 1 && pos.y - SheetManager.sheetManager.sheet.anchors[x][y + 1].transform.position.y < SheetManager.sheetManager.minLength)
            //{
            //    pos.y = SheetManager.sheetManager.sheet.anchors[x][y + 1].transform.position.y + SheetManager.sheetManager.minLength;
            //}
            //if (y > 0 && SheetManager.sheetManager.sheet.anchors[x][y - 1].transform.position.y - pos.y < SheetManager.sheetManager.minLength)
            //{
            //    pos.y = SheetManager.sheetManager.sheet.anchors[x][y - 1].transform.position.y - SheetManager.sheetManager.minLength;
            //}
            //if (x < SheetManager.sheetManager.sheet.xNum - 1 && SheetManager.sheetManager.sheet.anchors[x + 1][y].transform.position.x - pos.x < SheetManager.sheetManager.minLength)
            //{
            //    pos.x = SheetManager.sheetManager.sheet.anchors[x + 1][y].transform.position.x - SheetManager.sheetManager.minLength;
            //}
            //if (x > 0 && pos.x - SheetManager.sheetManager.sheet.anchors[x - 1][y].transform.position.x < SheetManager.sheetManager.minLength)
            //{
            //    pos.x = SheetManager.sheetManager.sheet.anchors[x - 1][y].transform.position.x + SheetManager.sheetManager.minLength;
            //}

            if (y < mySheet.yNum - 1 && pos.y - mySheet.anchors[x][y + 1].transform.position.y < SheetManager.sheetManager.minLength)
            {
                pos.y = mySheet.anchors[x][y + 1].transform.position.y + SheetManager.sheetManager.minLength;
            }
            if (y > 0 && mySheet.anchors[x][y - 1].transform.position.y - pos.y < SheetManager.sheetManager.minLength)
            {
                pos.y = mySheet.anchors[x][y - 1].transform.position.y - SheetManager.sheetManager.minLength;
            }
            if (x < mySheet.xNum - 1 && mySheet.anchors[x + 1][y].transform.position.x - pos.x < SheetManager.sheetManager.minLength)
            {
                pos.x = mySheet.anchors[x + 1][y].transform.position.x - SheetManager.sheetManager.minLength;
            }
            if (x > 0 && pos.x - mySheet.anchors[x - 1][y].transform.position.x < SheetManager.sheetManager.minLength)
            {
                pos.x = mySheet.anchors[x - 1][y].transform.position.x + SheetManager.sheetManager.minLength;
            }
            #endregion
            transform.position = pos;
            onDrag(this);
        }

        public override string ToString()
        {
            return "x:" + x + "," + "y:" + y;
        }
    }
}
